package top.yonyong.myjava.effectivejava;

/**
 * @Describtion 自己实现lombok builder
 * @Author yonyong
 * @Date 2020/7/24 10:33
 * @Version 1.0.0
 **/
public class MyBuilder {

    public static void main(String[] args) {
        User user = new User.Builder()
                .id(1)
                .name("老杨")
                .age(18)
                .sex('男')
                .build();

        System.out.println(user.toString());
    }

}

class User{
    private int id;
    private String name;
    private String idcard;
    private char sex;
    private int age;
    private double salary;

    private User(Builder builder) {
        id = builder.id;
        name = builder.name;
        idcard = builder.idcard;
        sex = builder.sex;
        age = builder.age;
        salary = builder.salary;
    }
    static class Builder{
        private int id;
        private String name;
        private String idcard;
        private char sex;
        private int age;
        private double salary;

        public Builder(){}

        public User build(){
            return new User(this);
        }

        public Builder id(int id){
            this.id = id;
            return this;
        }
        public Builder name(String name){
            this.name = name;
            return this;
        }
        public Builder idcard(String idcard){
            this.idcard = idcard;
            return this;
        }
        public Builder sex(char sex){
            this.sex = sex;
            return this;
        }
        public Builder age(int age){
            this.age = age;
            return this;
        }
        public Builder salary(Double salary){
            this.salary = salary;
            return this;
        }
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", idcard='" + idcard + '\'' +
                ", sex=" + sex +
                ", age=" + age +
                ", salary=" + salary +
                '}';
    }

}